
initMeasureUI();

/**
 * 生成测量图标
 */
function initMeasureUI() {
    var measureIcon = $('<div class="measure" onclick="startMeasure()"></div>');
    $('#toolPanel').append(measureIcon);
}

/**
 * 开始测量
 */
function startMeasure() {
    var div=document.createElement('div');
    div.id="message";
    document.body.appendChild(div);
    div.innerHTML=
        "<div class='measureWays' onclick='measureDis()'>测距离</div>" +
        "<div class='measureWays' onclick='measureArea()'>测面积</div>" +
        "<div id='removeMessage' onclick='removeParent(this)'>取消</div>";
}

function createCloseBtn() {
    var div=document.createElement("div");
    div.id="closeMeasure";
    div.textContent="结束测量";
    document.body.appendChild(div);
    return div;
}
//测距
function measureDis() {
    removeDiv("message");
    var p2=null;
    var p=null;
    var mlatlng=null;
    var num=0;
    var line=null;
    var popup=null;
    var aDis=0;
    var points=[];
    var cacheLayer=new L.FeatureGroup();
    var closeMbtn=createCloseBtn();
    closeMbtn.addEventListener("click",function () {
        map.removeLayer(cacheLayer);
        if(popup!=null){  map.removeLayer(popup);}
        map.removeEventListener("click");
        removeDiv("closeMeasure");
    });

    map.addEventListener("click",function (e) {
        switch (num){
            case 0:
                points.push([e.latlng.lat,e.latlng.lng]);

                mlatlng=L.latLng(e.latlng.lat,e.latlng.lng);
                num++;
                break;
            case 1:
                p2=[e.latlng.lat,e.latlng.lng];
                p=p2;
                num++;
                points.push([e.latlng.lat,e.latlng.lng]);
                line=L.polyline(points,{color:'red'});
                cacheLayer.addLayer(line);
                map.addLayer(cacheLayer);
//两点测距
                var point2=L.latLng(p2[0],p2[1]);
                var dis=mlatlng.distanceTo(point2);
                aDis=dis;
                dis=dis.toFixed(3);
//弹出值
                mlatlng=L.latLng(e.latlng.lat,e.latlng.lng);
                popup = L.popup()
                    .setLatLng(mlatlng)
                    .setContent('<p>长度(米)<p/>'+dis)
                    .openOn(map);
                break;
            case 2:
                //num=0;
                p=[e.latlng.lat,e.latlng.lng];
                points.push(p);
                map.removeLayer(cacheLayer);
                cacheLayer.removeLayer(line);

                var dis=countDis(p2,p);
                aDis=aDis+dis;

//弹出值
                mlatlng=L.latLng(e.latlng.lat,e.latlng.lng);
                popup = L.popup()
                    .setLatLng(mlatlng)
                    .setContent('<p>长度(米)<p/>'+aDis.toFixed(3))
                    .openOn(map);
                line=L.polyline(points,{color:'red'});
                cacheLayer.addLayer(line);
                map.addLayer(cacheLayer);
                p2=p;
                break;
            default:break;
        }
    });
}
function countDis(p1,p2) {
    var p3=L.latLng(p1[0],p1[1]);
    var p4=L.latLng(p2[0],p2[1]);
    return p3.distanceTo(p4);
}
//测面积
function measureArea() {
    removeDiv("message");
    var region=null;
    var latlngs=[];
    var popup=null;

    var cacheLayer=new L.FeatureGroup();

    var closeMbtn=createCloseBtn();
    closeMbtn.onclick=function () {
        map.removeEventListener("click");
        if(latlngs.length>1){
            map.removeLayer(cacheLayer);

        }
        if(popup!=null){map.removeLayer(popup);}
        removeDiv("closeMeasure");
    };
    map.addEventListener("click",function (e) {
        if(latlngs.length<2){
            latlngs.push([e.latlng.lat,e.latlng.lng]);
        }else{if(latlngs.length>2){
            map.removeLayer(cacheLayer);
            cacheLayer.removeLayer(region);
        }
            latlngs.push([e.latlng.lat,e.latlng.lng]);
            region=L.polygon(latlngs,{color:'red'});
            cacheLayer.addLayer(region);
            map.addLayer(cacheLayer);
            var areas=countArea(latlngs);
            areas=areas.toFixed(2);

            popup = L.popup()
                .setLatLng(region.getCenter())
                .setContent('<p>面积(平方米)<p/>'+areas)
                .openOn(map);
        }
    })
}
function countArea(latLngs) {
    var pointsCount = latLngs.length,
        area = 0.0,
        d2r = 0.017453292519943295769236907684886,
        p1, p2;
    for (var i = 0; i < pointsCount; i++) {
        p1 = latLngs[i];
        p2 = latLngs[(i + 1) % pointsCount];
        area += ((p2[1] - p1[1]) * d2r) *
            (2 + Math.sin(p1[0] * d2r) + Math.sin(p2[0] * d2r));
    }
    area = area * 6378137.0 * 6378137.0 / 2.0;
    return  Math.abs(area);
}

/**
 * 移除div
 * @param obj
 */
function removeParent(obj) {
    document.body.removeChild(obj.parentNode);
}